<?php
header("Content-Type:text/html;charset=utf-8");

//db的应用类
class DB{
	//定义成员属性
	public $server;
    public $dbname;
	public $username;
	public $password;
    Public $pdo;
	 // $pdo=new PDO("mysql:host=$server;dbname=$dbname",$password,$username);
	//定义构造方法
	function __construct($server,$dbname,$username,$password){
		$this->server=$server;
		$this->dbname = $dbname;
		$this->username=$username;
		$this->password= $password;

		//连接数据库
		$this->pdo =new PDO("mysql:host=".$this->server.";dbname=".$this->dbname."",$username,$password);
		$this->pdo->query("set names utf8");
	}
		//执行select语句，返回：二维数组
		public function queryAll($sql)
		{
			$result = array();
			$this->rs = $this->pdo->query($sql);
			while($row = $this->rs->fetch(PDO::FETCH_ASSOC))
			{
				$result[] = $row;
			}
			return $result;
		}

		//执行select语句，返回：一维关联数组
		public function queryOne($sql)
		{
			$result = NULL;
			$this->rs = $this->pdo->query($sql);
			if($row = $this->rs->fetch(PDO::FETCH_ASSOC))
			{
				$result = $row;
			}
			return $result;
		}
		//执行insert、update、delete语句，返回：受影响的行数
		public function exec($sql)
		{
			$re= $this->pdo->exec($sql);
			return $re;
		}
} 

$DB=new DB("localhost",'curl','root','root');
$pdo=$DB->pdo;


?>





